﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace LiveNet.Contacts
{
    public class EmailCollection 
    {
        #region Private Data
        private ArrayList emails = new ArrayList();
        #endregion

        #region Constructors
        internal EmailCollection()
        {
        }

        internal EmailCollection(XmlNode xmlnode)
        {
            FromXml(xmlnode);
        }
        #endregion

        #region Internal Methods
        internal void FromXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlNodeList nodes = xmlnode.SelectNodes("Email");
            if (nodes != null)
            {
                foreach (XmlNode node in nodes)
                {
                    Add(new Email(node));
                }
            }
        }

        internal void Add(Email email)
        {
            emails.Add(email);
        }

        internal void Remove(Email email)
        {
            emails.Remove(email);
        }

        internal void RemoveAll()
        {
            emails.Clear();
        }

        #endregion

        #region Public Methods
        public XmlDocument ToXml(OperationType op)
        {
            XmlDocument xmldoc = new XmlDocument();
            string xml = "<Emails>";
            foreach (Email email in this)
            {
                xml = xml + email.ToXml(op).InnerXml;
            }
            xml = xml + "</Emails>";
            xmldoc.LoadXml(xml);

            return xmldoc;
        }

        public IEnumerator GetEnumerator()
        {
            return emails.GetEnumerator();
        }

        public Email Find(int emailID)
        {
            foreach (Email email in emails)
            {
                if (email.ID == emailID) return email;
            }
            return null;
        }

        public Email New()
        {
            return new Email();
        }
        #endregion

        #region Public Properties
        public int Count
        {
            get { return emails.Count; }
        }

        public object[] ToArray
        {
            get { return emails.ToArray(); }
        }
        #endregion
    }
}
